package com.jwthhealth_pub.old.constitution.view.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jwthhealth_pub.R;
import com.jwthhealth_pub.old.api.ApiHelper;
import com.jwthhealth_pub.old.common.Constant;
import com.jwthhealth_pub.old.constitution.ConstitutionTypeUtil;
import com.jwthhealth_pub.old.constitution.model.ConstitutionCompleteModel;
import com.jwthhealth_pub.old.constitution.view.ConstitutionActivity;
import com.jwthhealth_pub.old.constitution.view.QuestionnaireActivity;
import com.jwthhealth_pub.old.sign.model.LoginModel;
import com.jwthhealth_pub.old.signutil.model.db.UserInfoDao;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by Administrator on 2016/12/14.
 */

public class ConstitutionAdapter extends RecyclerView.Adapter {

    private Context context;
    private ConstitutionActivity mActivity;

    public ConstitutionAdapter(Context context) {
        this.context = context;
        this.mActivity = (ConstitutionActivity) context;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_constituation, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        ViewHolder viewHolder = (ViewHolder) holder;
        switch (position){
            case 0:
                viewHolder.itemImg.setBackground(context.getResources().getDrawable(R.mipmap.constitution_menu_one));
                viewHolder.itemText.setText(context.getResources().getString(R.string.constitution_one));
                break;
            case 1:
                viewHolder.itemImg.setBackground(context.getResources().getDrawable(R.mipmap.constitution_menu_tow));
                viewHolder.itemText.setText(context.getResources().getString(R.string.constitution_two));
                break;
            case 2:
                viewHolder.itemImg.setBackground(context.getResources().getDrawable(R.mipmap.constitution_menu_thi));
                viewHolder.itemText.setText(context.getResources().getString(R.string.constitution_thr));
                break;
            case 3:
                viewHolder.itemImg.setBackground(context.getResources().getDrawable(R.mipmap.constitution_menu_for));
                viewHolder.itemText.setText(context.getResources().getString(R.string.constitution_for));
                break;
            case 4:
                viewHolder.itemImg.setBackground(context.getResources().getDrawable(R.mipmap.constitution_menu_fiv));
                viewHolder.itemText.setText(context.getResources().getString(R.string.constitution_fiv));
                break;
            case 5:
                viewHolder.itemImg.setBackground(context.getResources().getDrawable(R.mipmap.constitution_menu_six));
                viewHolder.itemText.setText(context.getResources().getString(R.string.constitution_six));
                break;
            case 6:
                viewHolder.itemImg.setBackground(context.getResources().getDrawable(R.mipmap.constitution_menu_sev));
                viewHolder.itemText.setText(context.getResources().getString(R.string.constitution_sev));
                break;
            case 7:
                viewHolder.itemImg.setBackground(context.getResources().getDrawable(R.mipmap.constitution_menu_eig));
                viewHolder.itemText.setText(context.getResources().getString(R.string.constitution_eig));
                break;
            case 8:
                viewHolder.itemImg.setBackground(context.getResources().getDrawable(R.mipmap.constitution_menu_nin));
                viewHolder.itemText.setText(context.getResources().getString(R.string.constitution_nin));
                break;
            default:break;
        }

        viewHolder.itemLayout.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                if (position == 0) {
                    Call<ConstitutionCompleteModel> constitutionCompleteModelCall;
                    LoginModel.DataBean userinfoModel = UserInfoDao.getInstance().queryUserinfoModel();
                    if (userinfoModel == null) {
                        constitutionCompleteModelCall = ApiHelper.constitutionComplete(null, null);

                    } else {
                        constitutionCompleteModelCall = ApiHelper.constitutionComplete(userinfoModel.getId(), userinfoModel.getAndroidtoken());
                    }

                    constitutionCompleteModelCall.enqueue(new Callback<ConstitutionCompleteModel>() {
                        @Override
                        public void onResponse(Call<ConstitutionCompleteModel> call, Response<ConstitutionCompleteModel> response) {
                            if (response.body().getCode().equals("0")) {
                                Intent intent = new Intent(context, QuestionnaireActivity.class);
                                intent.putExtra(Constant.CONSTITUTION_TYPE, position);
                                context.startActivity(intent);
                            } else {
                                List<String> typeList = response.body().getData().getList();
                                String unCompleteTypeList = "";
                                for (int i=0; i < typeList.size(); i++) {
                                    unCompleteTypeList = unCompleteTypeList + ConstitutionTypeUtil.getTypeName(typeList.get(i)) +" ";
                                }

                                Message message = new Message();
                                Bundle bundle = new Bundle();
                                bundle.putString(ConstitutionActivity.UNCOMPLETETYPE, unCompleteTypeList);
                                message.setData(bundle);

                                mActivity.mHandler.sendMessage(message);
                            }
                        }

                        @Override
                        public void onFailure(Call<ConstitutionCompleteModel> call, Throwable t) {
                            Log.d("ConstitutionAdapter", "fail");
                        }
                    });
                } else {
                    Intent intent = new Intent(context, QuestionnaireActivity.class);
                    intent.putExtra(Constant.CONSTITUTION_TYPE, position);
                    context.startActivity(intent);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return 9;
    }

    class ViewHolder extends RecyclerView.ViewHolder{

        private final RelativeLayout itemLayout;
        private final TextView itemText;
        private final ImageView itemImg;

        public ViewHolder(View itemView) {
            super(itemView);
            itemText = (TextView) itemView.findViewById(R.id.item_text);
            itemImg = (ImageView) itemView.findViewById(R.id.item_img);
            itemLayout = (RelativeLayout) itemView.findViewById(R.id.item_layout);
        }
    }
}
